package com.yixueyun.content.service.jobHandler;

import com.yixueyun.base.exception.YiXueYunPlusException;
import com.yixueyun.content.feigncient.SearchServiceClient;
import com.yixueyun.content.mapper.CoursePublishMapper;
import com.yixueyun.content.model.po.CourseIndex;
import com.yixueyun.content.model.po.CoursePublish;
import com.yixueyun.content.service.CoursePublishService;
import com.yixueyun.messagesdk.model.po.MqMessage;
import com.yixueyun.messagesdk.service.MessageProcessAbstract;
import com.yixueyun.messagesdk.service.MqMessageService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

@Slf4j
@Component
public class CoursePublishTask extends MessageProcessAbstract {

    @Autowired
    CoursePublishService coursePublishService;
    @Autowired
    SearchServiceClient searchServiceClient;
    @Autowired
    CoursePublishMapper coursePublishMapper;
    @XxlJob("CoursePublishJobHandler")
    public void coursePublishJobHandler() {
        //分片参数
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        process(shardIndex,shardTotal,"course_publish",30,60);
    }
    @Override
    public boolean execute(MqMessage mqMessage) {
        //得到课程id
        long courseId = Long.parseLong(mqMessage.getBusinessKey1());

        //静态页面上传到minio
        generateCourseHtml(mqMessage,courseId);

        //向es写入索引
        saveCourseIndex(mqMessage,courseId);
        //向redis写入课程信息

        return true;
    }
    //页面静态化
    private void generateCourseHtml(MqMessage mqMessage,Long courseId){
        Long taskId = mqMessage.getId();
        MqMessageService mqMessageService = this.getMqMessageService();
        //幂等性判断
        int stageOne = mqMessageService.getStageOne(taskId);
        if(stageOne > 0) {
            log.debug("课程静态化已完成，无需处理...");
            return;
        }
        //处理任务
        File file = coursePublishService.generateCourseHtml(courseId);
        if(file == null) {
            YiXueYunPlusException.cast("生成静态页面为空");
        }
        //上传到minio
        coursePublishService.uploadCourseHtml(courseId,file);
        //更新状态
        mqMessageService.completedStageOne(taskId);
    }
    private void saveCourseIndex(MqMessage mqMessage,Long courseId){
        Long taskId = mqMessage.getId();
        MqMessageService mqMessageService = this.getMqMessageService();
        //幂等性判断
        int stageTwo = mqMessageService.getStageTwo(taskId);
        if(stageTwo > 0) {
            log.debug("课程索引任务已完成，无需处理...");
            return;
        }
        //处理任务
        //查询课程信息
        CoursePublish coursePublish = coursePublishMapper.selectById(courseId);
        CourseIndex courseIndex = new CourseIndex();
        BeanUtils.copyProperties(coursePublish,courseIndex);
        Boolean add = searchServiceClient.add(courseIndex);
        if(!add) {
            YiXueYunPlusException.cast("添加索引失败，发生了熔断");
        }

        //更新状态
        mqMessageService.completedStageTwo(taskId);

    }
}
